package dk.dashboardFP.panels.loggerPanel {
	import dk.dashboardFP.dfpLogger.DFPLogger;
	import dk.dashboardFP.modelDataHandler.XMLDataProperty;
	import dk.dashboardFP.panels.subPanels.AbsDashboardFPSubPanel;
	import dk.dashboardFP.panels.subPanels.WindowSubPanel;
	import dk.dashboardFP.panels.subPanels.WindowSubPanelModel;
	import dk.dashboardFP.utils.XMLDataHolder;

	/**
	 * @author josef
	 */
	public class FilterWindowModel extends WindowSubPanelModel {
		private var _sHideStateKey : String = "hideStateKey";
		private var _oFilterHideStateProp : XMLDataProperty;
		private var _xmlFilterGroups : XML;
		private var _oFilterWindowSubPanel : FilterWindowSubPanel;

		public function FilterWindowModel(oSubPanel : AbsDashboardFPSubPanel, oFilterWindowSubPanel : FilterWindowSubPanel) {
			_oFilterWindowSubPanel = oFilterWindowSubPanel;
			super(oSubPanel as WindowSubPanel);
		}

		override protected function setup() : void {

			super.setup();

			//:::::::
			// filter hide state 
			_oFilterHideStateProp = _oMDH.get_oXMLDataProperty(_sHideStateKey);
			
			//:::::::
			// register
			_oFilterWindowSubPanel.addEventListener(FilterWindowSubPanelEvent.NEW_FILTER, onNewFilterAdded, false, 0, true);
			
			//:::::::
			// init 
			initFilterProp(); 
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// new filter
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onNewFilterAdded(event : FilterWindowSubPanelEvent) : void {
			var doLogFilterItem : LogFilterItem = event.oData as LogFilterItem;

			doLogFilterItem.addEventListener(LogFilterItemEvent.REQUEST_CHANGE, onFilterItemChangeRequest, false, 0, true);
			doLogFilterItem.addEventListener(LogFilterItemEvent.LEVEL_CHANGE, onFilterGroupLevelChange, false, 0, true);

			addFilterGroupToXML(doLogFilterItem);
			
			updateLogFilterItems();
		}

		private function onFilterItemChangeRequest(event : LogFilterItemEvent) : void {

			var xmlGroup : XML = XML(_xmlFilterGroups.*.(@id == LogFilterItem(event.target).oLogGroup.groupId));
			
			if (xmlGroup.@visible == "false") {
				xmlGroup.@visible = true;
			} else {
				xmlGroup.@visible = false;
			}

			_oFilterHideStateProp.dispatchUpdate(false);
			updateLogFilterItems();
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// filter group level chnge 
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function onFilterGroupLevelChange(event : LogFilterItemEvent) : void {
			
			var xmlGroup : XML = XML(_xmlFilterGroups.*.(@id == LogFilterItem(event.target).oLogGroup.groupId));
			xmlGroup.@level = event.data;
			_oFilterHideStateProp.dispatchUpdate(false);
			updateLogFilterItems();
			DFPLogger.log("change group level filter", -1, xmlGroup);
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// update filter items
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function updateLogFilterItems(bDispatchChange : Boolean = false) : void {
			for each (var xmlGroup : XML in _xmlFilterGroups.group) {
			
				var oGroup : XMLDataHolder = new XMLDataHolder(xmlGroup);
				
				var doLogFilterItem : LogFilterItem = _oFilterWindowSubPanel.o_doLogFilterItems[xmlGroup.@id] as LogFilterItem;
				
				if (doLogFilterItem) {
					
					// visibe 
					doLogFilterItem.oCB.set_value(oGroup.get_bParmByKey("visible"), bDispatchChange);
					doLogFilterItem.oLogGroup.visible = oGroup.get_bParmByKey("visible");
					
					// log level 
					doLogFilterItem.logLevel = xmlGroup.@level;
					doLogFilterItem.oLogGroup.level = xmlGroup.@level;
				}
			}
			
			dispatchEvent(new FilterWindowModelEvent(FilterWindowModelEvent.FILTERS_UPDATED));
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// xml change
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function initFilterProp() : void {
			
			if (!_xmlFilterGroups) {
				
				if (_oFilterHideStateProp.xmlDefault.filterGroups == undefined) {
					_oFilterHideStateProp.xmlDefault.appendChild(<filterGroups/>);
				}
			
				_xmlFilterGroups = XML(_oFilterHideStateProp.xmlDefault.filterGroups);
				
				//:::::::
				// check for new groups 
				for (var s : String in _oFilterWindowSubPanel.o_doLogFilterItems) {
					var doLogFilterItem : LogFilterItem = _oFilterWindowSubPanel.o_doLogFilterItems[s];
					addFilterGroupToXML(doLogFilterItem);
				}
			}
			
			updateLogFilterItems();
		}

		private function addFilterGroupToXML(doLogFilterItem : LogFilterItem) : void {
						
			if (_xmlFilterGroups.*.(@id == doLogFilterItem.oLogGroup.groupId) == undefined) {
				
				var xmlFilterGroup : XML = XML(<group/>);
				xmlFilterGroup.@id = doLogFilterItem.oLogGroup.groupId;
				xmlFilterGroup.@visible = true;
				xmlFilterGroup.@level = 0;
		
				_xmlFilterGroups.appendChild(xmlFilterGroup);
				_oFilterHideStateProp.dispatchUpdate(false);
			}
		}

		public function get oFilterHideStateProp() : XMLDataProperty {
			return _oFilterHideStateProp;
		}

		public function get xmlFilterGroups() : XML {
			return _xmlFilterGroups;
		}
	}
}
